using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using DIS_DAO.DAO;
using DIS_DAO.Helpers;

namespace DIS_DAO.DTO
{
	/// <summary>
	/// Data transfer object which is associated with Employee table in database
	/// </summary>
	public class EmployeeDTO
	{
		public int ID { get; set; }
		public int IDRole { get; set; }
		public int IDChemistWareHouse { get; set; }
		public string UserName { get; set; }
		public string Password { get; set; }
		public string FullName { get; set; }
        public ChemistWareHouseDTO ChemistWareHouse { get; set; }
        public RoleDTO Role { get; set; }

		public static EmployeeDTO Translate(DataRow row)
		{
			var dto = new EmployeeDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				IDRole = int.Parse(row["IDRole"].ToString()),
				UserName = row["UserName"].ToString(),
				Password = row["Password"].ToString(),
				FullName = row["FullName"].ToString(),
			};
            if (row["IDChemistWareHouse"].ToString() != string.Empty)
            {
                dto.IDChemistWareHouse = int.Parse(row["IDChemistWareHouse"].ToString());

                var rpt = new ChemistWareHouseDAO(GlobalVariables.DbConnectionString);
                var objDS = rpt.SelectByID(dto.IDChemistWareHouse);
                if (objDS != null)
                {
                    if (objDS.Tables[0].Rows.Count > 0)
                    {
                        dto.ChemistWareHouse = ChemistWareHouseDTO.Translate(objDS.Tables[0].Rows[0]);
                    }
                }
            }

            var rpt2 = new RoleDAO(GlobalVariables.DbConnectionString);
            var objDS2 = rpt2.SelectByID(dto.IDRole);
            if (objDS2 != null)
            {
                if (objDS2.Tables[0].Rows.Count > 0)
                {
                    dto.Role = RoleDTO.Translate(objDS2.Tables[0].Rows[0]);
                }
            }

			return dto;
		}
	}
}